//
// Created by gogoqiu on 2022/1/11.
//

#include "MainDialog.h"
#include "wx/wx.h"
#include "wx/confbase.h"
#include "wx/stdpaths.h"
#include "wx/sysopt.h"
#include <wx/fs_zip.h> // ZIP filesystem support
#include <wx/tipdlg.h>
#include <wx/cshelp.h>
#include <wx/image.h>
#include "view/PreferenceAdapter.h"
#include "MainDialog.h"


//
BEGIN_EVENT_TABLE ( MainDialog, wxFrame )
                //EVT_MENU ( ACTION_EVENT, PreferencesFrame::OnActionEvent )
END_EVENT_TABLE()


//如何去掉
//动态添加配置?
/**
 * wxDEFAULT_FRAME_STYLE
 * wxDEFAULT_FRAME_STYLE ^ wxMAXIMIZE_BOX ^ wxRESIZE_BORDER
 *
 * @param _myTagApp
 */
MainDialog::MainDialog( wxWindow *parent, MyTagApp * _myTagApp )
        : wxFrame( parent, wxID_ANY, "配置", wxDefaultPosition, wxDefaultSize,
                   wxDEFAULT_FRAME_STYLE ^ wxMAXIMIZE_BOX ^ wxRESIZE_BORDER )
{
    this->myTagApp = _myTagApp;
    auto *menuFile = new wxMenu;
    //
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                     "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    auto *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    auto *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar( menuBar );
    //
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
    //
    Bind(wxEVT_MENU, &MainDialog::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MainDialog::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MainDialog::OnExit, this, wxID_EXIT);
    SetClientSize(wxSize(350,500));
    //
    dialogRootBoxSizer = new wxBoxSizer(wxVERTICAL);
    m_listbox = new wxScrolledWindow(this, wxID_ANY,
                                     wxDefaultPosition, wxDefaultSize, wxHSCROLL | wxVSCROLL );
    //m_listbox: panelRootBoxSizer
    //panels: innerSizer
    m_listbox->SetScrollRate(5, 5 );
    //
    dialogRootBoxSizer->Add(m_listbox, 1, wxEXPAND, 0 );
    this->SetSizer(dialogRootBoxSizer);
    //m_listbox->SetBackgroundColour(wxColour(0, 128, 192 ) );
    //return;
    //wxBoxSizer *BoxSizer = new wxBoxSizer(wxHORIZONTAL);
    //panelRootBoxSizer = new wxBoxSizer(wxVERTICAL);

    //
    //BuildView();

    mPreferenceAdapter = new PreferenceAdapter( myTagApp, this, m_listbox );

    //connect this::close to this::dismiss
    //https://blog.csdn.net/sq8706/article/details/119087505
    //wxWidgets 关闭按钮
    //动态设置的
    Bind(wxEVT_CLOSE_WINDOW,wxCloseEventHandler(MainDialog::OnClose),this);
    //bind the request data to the this frame on the data
    //PreferenceAdapter::OnActionEvent ( wxCommandEvent & event )
    /*
     * void Bind(const EventTag &eventType,
              void (Class::*method)(EventArg &),
              EventHandler *handler,
              int winid = wxID_ANY,
    EVT_MENU ( ACTION_EVENT, MainFrame::OnActionEvent )
     */
    //Bind( ACTION_EVENT, wxCommandEventHandler(PreferencesFrame::OnActionEvent ), this );
    request();
}


MainDialog::MainDialog(MyTagApp *_myTagApp, const wxString &title, const wxLocale &locale)
        : wxFrame( NULL, wxID_ANY, "配置", wxDefaultPosition, wxDefaultSize,
                   wxDEFAULT_FRAME_STYLE ^ wxMAXIMIZE_BOX ^ wxRESIZE_BORDER )
                   {
    this->myTagApp = _myTagApp;
    auto *menuFile = new wxMenu;
    //
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                     "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);

    auto *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);

    auto *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar( menuBar );
    CreateStatusBar();

    SetStatusText("Welcome to wxWidgets!");
    //
    Bind(wxEVT_MENU, &MainDialog::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MainDialog::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MainDialog::OnExit, this, wxID_EXIT);
    //
    SetClientSize(wxSize(350,500));
    //
    dialogRootBoxSizer = new wxBoxSizer(wxVERTICAL);
    m_listbox = new wxScrolledWindow(this, wxID_ANY,
                                     wxDefaultPosition, wxDefaultSize, wxHSCROLL | wxVSCROLL );
    //m_listbox: panelRootBoxSizer
    //panels: innerSizer
    m_listbox->SetScrollRate(5, 5 );
    //
    dialogRootBoxSizer->Add(m_listbox, 1, wxEXPAND, 0 );
    this->SetSizer(dialogRootBoxSizer);
    //mPreferenceAdapter = new PreferenceAdapter( myTagApp, this, m_listbox );

    //Bind(wxEVT_CLOSE_WINDOW,wxCloseEventHandler(MainDialog::OnClose),this);


}

/*
 * background service
 * onInit的时候启动，关闭时关闭。ui上可以看见状态
 */
void MainDialog::request(){
    vector< PreferenceView*> items = myTagApp->getPreferenceCore()->getPreferenceViews();
    mPreferenceAdapter->setItemsWithNotify( items );
    //this->SetSizer(dialogRootBoxSizer);
    //Layout();
}

void MainDialog::OnClose( wxCloseEvent&  event ){
    this->Show( false );
}

MainDialog::~MainDialog(){
    for( int i=0;i <panels.size(); i++ ){
        //panels.at(i)->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( PreferencesFrame::OnClickItem ), NULL, this );
    }

}

void MainDialog::OnExit(wxCommandEvent& event)
{
    Close(true);
}

void MainDialog::OnAbout(wxCommandEvent& event)
{
    wxMessageBox("This is a wxWidgets Hello World example",
                 "About Hello World", wxOK | wxICON_INFORMATION);
}

void MainDialog::OnHello(wxCommandEvent& event)
{
    wxLogMessage("Hello world from wxWidgets!");
}

void MainDialog::OnClickItem(wxMouseEvent &event) {

}

void MainDialog::OnActionEvent(wxCommandEvent &event) {

}

/*
 * progressbar
 *
*/
void MainDialog::startService() {

}








//service thread

//post file thread

